#!/usr/bin/python

import examples.basegame
import game.entity
import game.gameworld
import ai.steer.engine as steer
import euclid
import random
import events.dispatcher
import pygame
import render

class WanderBoid(examples.basegame.DemoBoid):
    RED = (255, 0, 0 )

    def __init__(self, window, pos, mass, maxspeed, maxforce, maxturnrate):
        super( WanderBoid, self ).__init__( window, pos, mass, maxspeed, maxforce, maxturnrate )

        self.wander = steer.Wander( 10, 10, 10 )
        self.steer.add( self.wander )

    def draw(self):
        super( WanderBoid, self ).draw()
        render.textrenderer.instance().render(
        "Dist: %.2f Radius: %.2f Jitter: %.2f" %( self.wander.distance, self.wander.radius, self.wander.jitter ),
        (7, 40))

        center = self.pos + self.heading.normalized() * self.wander.distance
        pygame.gfxdraw.circle( self.window.screen, center.x, center.y, self.wander.radius, self.RED )
        tw = self.transform * self.wander.target
        pygame.gfxdraw.circle( self.window.screen, tw.x, tw.y, 5, self.RED )
        pygame.gfxdraw.line( self.window.screen, self.pos.x, self.pos.y, tw.x, tw.y, self.RED )

class PursuitBoid(game.entity.Boid):
    def __init__(self, target, window, pos, mass, maxspeed, maxforce, maxturnrate):
        super( PursuitBoid, self ).__init__( window, pos, mass, maxspeed, maxforce, maxturnrate )
        self.colour = ( 0, 0, 255 )
        self.pursuit = steer.Pursuit( target )
        self.steer.add( self.pursuit )
        
    def draw(self):
        super(PursuitBoid, self).draw()
        pygame.gfxdraw.line( self.window.screen, self.pos.x, self.pos.y,
                             self.pursuit.target.pos.x, self.pursuit.target.pos.y, (255, 0, 0) )

class PursuitDemo( examples.basegame.BaseGame ):
    def setup(self):
        super(PursuitDemo, self).setup()
        pos1 =  euclid.Vector3(
            random.uniform( 0, self.window.width ),
            random.uniform( 0, self.window.height ),
            0 )

        pos2 =  euclid.Vector3(
            random.uniform( 0, self.window.width ),
            random.uniform( 0, self.window.height ),
            0 )


        self.boid = WanderBoid( self.window, pos1, 10, 50, 50, 50 )
        self.pursuier = PursuitBoid( self.boid, self.window, pos2, 10, 50, 50, 50 )
        self.mark = None


    def _onKey( self, event ):
        if event.key == pygame.K_q: self.boid.mass += 1
        elif event.key == pygame.K_a:
            self.boid.mass -= 1
            self.boid.mass = max( 1, self.boid.mass )
        elif event.key == pygame.K_w: self.boid.maxForce += 1
        elif event.key == pygame.K_s:
            self.boid.maxForce -= 1
            self.boid.maxForce = max( 1, self.boid.maxForce )
        elif event.key == pygame.K_e: self.boid.maxSpeed += 1
        elif event.key == pygame.K_d:
            self.boid.maxSpeed -= 1
            self.boid.maxSpeed = max( 1, self.boid.maxSpeed )
        elif event.key == pygame.K_r: self.boid.maxTurnRate += 1
        elif event.key == pygame.K_f:
            self.boid.maxTurnRate -= 1
            self.boid.maxTurnRate = max( 1, self.boid.maxTurnRate )
        elif event.key == pygame.K_t: self.boid.wander.radius += 1
        elif event.key == pygame.K_g:
            self.boid.wander.radius -= 1
            self.boid.wander.radius = max( 1, self.boid.wander.radius )
        elif event.key == pygame.K_y: self.boid.wander.distance += 1
        elif event.key == pygame.K_h:
            self.boid.wander.distance -= 1
            self.boid.wander.distance  = max( 0, self.boid.wander.distance )
        elif event.key == pygame.K_u: self.boid.wander.jitter += 1
        elif event.key == pygame.K_j:
            self.boid.wander.jitter -= 1
            self.boid.wander.jitter = max( 0, self.boid.wander.jitter )
        else:
             super(PursuitDemo, self)._onKey(event)


